﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Markup;
using System.Xml;

namespace Common.Core.Serialization
{
	public class XamlSerializationHelper
	{
		public ResultType Create<ResultType>( Type type, string name )
		{
			var result = Create<ResultType>( type.Assembly, name );
			return result;
		}

		public ResultType Create<ResultType>( Assembly assembly, string name )
		{
			using ( var stream = assembly.GetManifestResourceStream( name ) )
			{
				var result = stream != null ? (ResultType)XamlReader.Load( stream ) : default(ResultType);
				return result;
			}
		}

		public ResultType Create<ResultType>( string filePath )
		{
			var xaml = File.ReadAllText( filePath );
			var result = CreateFromXaml<ResultType>( xaml );
			return result;
		}

		public ResultType CreateFromXaml<ResultType>( string xaml )
		{
			var result = (ResultType)XamlReader.Parse( xaml );
			return result;
		}
	}
}
